const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';
import {
  showMsg
} from '../../../../utils/config.js'
import {
  reqConsumerRoomSort,
  reqConsumerRoomDel
} from '../../../../request/api.js';
Page({
  data: {
    navMarginTop: app.globalData.navMarginTop + 'px', //导航栏高度
    navBarHeight: app.globalData.navBarHeight + 'px', //头部标题高度
    navHeight: app.globalData.navHeight + 'px', //头部距离
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离

    dataList: [],

  },

  onLoad(options) {
    this.getParams()
  },


  onShow() {

  },

  getParams() {
    const _this = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('sendDataToEdit', function (res) {
      _this.setData({
        dataList: res.data
      })
      console.log('dataList', _this.data.dataList);
    })
  },

  onMoveTop(e) {
    const {
      item
    } = e.currentTarget.dataset
    const {
      dataList,
    } = this.data;

    const itemIndex = dataList.findIndex(data => data.id === item.id);
    const updatedDataList = [
      item, // 新的第一个元素
      ...dataList.slice(0, itemIndex), // item 之前的元素
      ...dataList.slice(itemIndex + 1) // item 之后的元素
    ];

    this.setData({
      dataList: updatedDataList
    });
  },

  async onDel(e) {
    const {
      item
    } = e.currentTarget.dataset
    console.log('item', item);
    const {
      dataList,
    } = this.data;


    if (item.deviceCount) {
      Dialog.confirm({
          title: '温馨提示',
          message: '房间内的设备不会被删除，可以在“所有设备”中找到未归属房间的设备',
        })
        .then(async () => {
          const res = await reqConsumerRoomDel({
            roomIdList: [item.id],
          })
          if (res.code != 20000) return showMsg(res.message);
          const itemIndex = dataList.findIndex(data => data.id === item.id);
          dataList.splice(itemIndex, 1)
          this.setData({
            dataList
          })
          app.globalData.isUpdateCIndex = true
          wx.setStorageSync('isUpdateCIndex', true);
          this.notifyPageUpdataFoo()
        })
        .catch(() => {});
    } else {

      const res = await reqConsumerRoomDel({
        roomIdList: [item.id],
      })
      if (res.code != 20000) return showMsg(res.message);
      const itemIndex = dataList.findIndex(data => data.id === item.id);
      dataList.splice(itemIndex, 1)
      this.setData({
        dataList
      })
      app.globalData.isUpdateCIndex = true
      wx.setStorageSync('isUpdateCIndex', true);
      this.notifyPageUpdataFoo()
    }

  },

  async onFinish() {
    const {
      dataList
    } = this.data
    if(dataList.length==0){
      app.globalData.isUpdateCIndex = true
      wx.setStorageSync('isUpdateCIndex', true);
      wx.navigateBack()
      this.notifyPageUpdataFoo()
      return;
    }
    const roomIds = dataList.map(item => item.id)
    const res = await reqConsumerRoomSort({
      roomIds,
    })
    if (res.code != 20000) return showMsg(res.message);
    app.globalData.isUpdateCIndex = true
    wx.setStorageSync('isUpdateCIndex', true);
    wx.navigateBack()
    this.notifyPageUpdataFoo()
  },

  notifyPageUpdataFoo() {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('receiveDataFromEdit', {
      isUpdataList: true
    });
  },

})